import 'package:flutter/material.dart';

/// 所有页面基类，建议所有页面继承这个类
abstract class BaseStatefulPage extends StatefulWidget {

  /// 页面名称，默认类名
  final String pageName;
  /// 构造函数，支持传入页面名
  const BaseStatefulPage({Key? key, String? pageName})
      : pageName = pageName ?? '',
        super(key: key);

  @override
  BaseStatefulPageState createState();
}

/// 对应的 State 基类，泛型约束
abstract class BaseStatefulPageState<T extends BaseStatefulPage>
    extends State<T> with WidgetsBindingObserver {
  double  screenWidth=0;
  double  screenHeight=0;
  bool _isMounted = false;
  late BuildContext context;
  /// 是否页面已经装载
  bool get isMounted => _isMounted;

  /// 页面是否可见（App生命周期）
  bool _isVisible = true;

  /// 是否页面可见
  bool get isVisible => _isVisible;

  @override
  void initState() {
    super.initState();
    _isMounted = true;
    WidgetsBinding.instance.addObserver(this);

    debugPrint('BaseStatefulPage:【${widget.pageName.isNotEmpty ? widget.pageName : widget.runtimeType}】 initState');

  }




  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint('BaseStatefulPage:【${widget.pageName.isNotEmpty ? widget.pageName : widget.runtimeType}】 didChangeDependencies');
  }

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
    debugPrint('BaseStatefulPage:【${widget.pageName.isNotEmpty ? widget.pageName : widget.runtimeType}】 didUpdateWidget');
  }

  @override
  void deactivate() {
    super.deactivate();
    debugPrint('BaseStatefulPage:【${widget.pageName.isNotEmpty ? widget.pageName : widget.runtimeType}】 deactivate');
  }

  @override
  void dispose() {
    debugPrint('BaseStatefulPage:【${widget.pageName.isNotEmpty ? widget.pageName : widget.runtimeType}】 dispose');
    WidgetsBinding.instance.removeObserver(this);
    _isMounted = false;
    onDispose();
    super.dispose();
  }

  /// 子类实现资源释放逻辑
  @protected
  void onDispose() {}

  /// 监听 App 生命周期切换（前台/后台）
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    debugPrint('BaseStatefulPage:【${widget.pageName}】 AppLifecycleState: $state');

    if (state == AppLifecycleState.resumed) {
      _isVisible = true;
      didChangeAppLifecycleStateOnResume();
    } else {
      _isVisible = false;
      didChangeAppLifecycleStateOnPause();
    }
  }

  /// 子类可重写恢复可见回调
  @protected
  void didChangeAppLifecycleStateOnResume() {
    debugPrint("BaseStatefulPage: onResume 页面恢复可见");
  }

  /// 子类可重写暂停回调
  @protected
  void didChangeAppLifecycleStateOnPause() {
    debugPrint("BaseStatefulPage: onPause 页面进入后台");
  }

  /// 统一调用 setState，确保 mounted 状态安全
  @protected
  void safeSetState(VoidCallback fn) {
    if (mounted) {
      debugPrint('BaseStatefulPage: setState');
      setState(fn);
    } else {
      debugPrint('BaseStatefulPage: 【${widget.pageName}】 safeSetState: widget not mounted');
    }
  }

  /// 子类实现页面主体内容
  @protected
  Widget buildPage(BuildContext context);

  @override
  Widget build(BuildContext context) {
    screenWidth = MediaQuery.of(context).size.width;
    screenHeight = MediaQuery.of(context).size.height;
    this.context = context;
    return buildPage(context);
  }

  /// 统一的错误界面
  @protected
  Widget buildErrorView(String? error) {
    return Center(child: Text(error ?? '发生错误'));
  }

  /// 统一的空数据界面
  @protected
  Widget buildEmptyView() {
    return const Center(child: Text('暂无数据'));
  }
}
